package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.dao.TeamBuildingDao;
import com.orhon.smartcampus.modules.teacher.entity.TeamBuilding;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLTeamBuildingInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.TeamBuildingInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddTeamBuildingMutation implements DataFetcher {

    @Autowired
    TeamBuildingDao teamBuildingDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 教师新增/修改接口
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLTeamBuildingInput input = TeamBuildingInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        TeamBuilding teamBuilding = null;

        if (input.getId() != null) {
            teamBuilding = teamBuildingDao.get(TeamBuilding.class, input.getId());
            teamBuilding.setUpdatedAt(new Date());
        } else {
            teamBuilding = new TeamBuilding();
            teamBuilding.setCreatedAt(new Date());
        }
        if(schoolId != null){

            teamBuilding.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            teamBuilding.setTeacher_id(input.getTeacher_id());
        }
        if(input.getMemberid() != null){

            teamBuilding.setMemberid(input.getMemberid());
        }
        if(input.getMemberphone() != null){

            teamBuilding.setMemberphone(input.getMemberphone());
        }
        if(input.getImage() != null){

            teamBuilding.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            teamBuilding.setShenhe(input.getShenhe());
        }
        if(input.getCommonproject() != null){

            teamBuilding.setCommonproject(JSON.toJSONString(input.getCommonproject()));
        }
        if(input.getMembername() != null){

            teamBuilding.setMembername(JSON.toJSONString(input.getMembername()));
        }
        if(input.getMembereducation() != null){

            teamBuilding.setMembereducation(JSON.toJSONString(input.getMembereducation()));
        }
        if(input.getMemberpost() != null){

            teamBuilding.setMemberpost(JSON.toJSONString(input.getMemberpost()));
        }
        try{
            teamBuildingDao.saveOrUpdate(teamBuilding);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

